package com.online.edu.eduservice.entity;


import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

//定义具体的数据返回格式
@Data
public class R implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final int SUCCESS = 20000;//成功响应码
    public static final int AUTH = 30000;//没有权限
    public static final int FAIL = 40000;//失败响应码


    //链式编程
    private Boolean success;
    private Integer code;
    private String message;
    private Map<String, Object> data = new HashMap<>();

    private R() {
    }

    //操作成功，调用这个方法，返回成功的数据
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(SUCCESS);
        r.setMessage("操作成功");
        return r;
    }

    //操作失败，调用这个方法，返回失败的数据
    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(FAIL);
        r.setMessage("操作失败");
        return r;
    }

    //使用链式编程
    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
